import api from '../../api';
import { message } from 'antd';
import dictionaryConfig from '../../utils/dictionaryConfig';
import accounting from 'accounting';

export default {
  namespace: 'ValuationCheckM',
  state: {
    positionColumns: [
      {
        dataIndex: 'dataDt',
        key: 'dataDt',
        title: '对比日期',
        type: 'date',
        width: 120,
      },
      {
        dataIndex: 'fundcode',
        key: 'fundcode',
        title: '产品代码',
        width: 180,
        render: (val, record) => {
          if (record.isExpire == 0) {
            return <span style={{ color: 'red' }}>{val}</span>;
          } else {
            return val;
          }
        },
      },
      {
        dataIndex: 'fundname',
        key: 'fundname',
        width: 180,
        title: '产品名称',
      },
      {
        dataIndex: 'tradingcode',
        key: 'tradingcode',
        width: 180,
        title: '证券代码',
      },
      {
        dataIndex: 'zqName',
        key: 'zqName',
        width: 180,
        title: '证券名称',
      },
      {
        dataIndex: 'exchangeCode',
        key: 'exchangeCode',
        title: '交易市场',
        width: 100,
      },
      {
        dataIndex: 'type',
        key: 'type',
        title: '资产类型',
        width: 100,
        render: (text, record, index) => {
          if (record.type == 32 || record.type == 33) {
            // eslint-disable-next-line no-useless-concat
            return '银行间买断式' + `${record.type == 32 ? '融资回购' : '融券回购'}`;
          }
          if (text) {
            let result = dictionaryConfig.holdType.filter((item) => item.value == text);
            if (result && result.length > 0) {
              return result[0].title;
            } else {
              return '--';
            }
          } else {
            return '--';
          }
        },
      },
      {
        dataIndex: 'trdAttr',
        key: 'trdAttr',
        title: '交易属性',
        width: 100,
      },
      {
        dataIndex: 'cz',
        key: 'cz',
        type: 'number',
        toFixed: 2,
        title: '差值',
        width: 120,
        render: (text) => {
          return accounting.formatNumber(Math.abs(text), 2);
        },
      },
      {
        dataIndex: 'xtsl',
        key: 'xtsl',
        type: 'number',
        toFixed: 2,
        width: 120,
        title: '系统数量',
      },
      {
        dataIndex: 'cwsl',
        key: 'cwsl',
        type: 'number',
        toFixed: 2,
        width: 120,
        title: '财务数量',
      },
      {
        dataIndex: 'operate',
        key: 'operate',
        type: 'operate',
        width: 200,
        title: '操作',
      },
    ],
    fundColumns: [
      {
        dataIndex: 'dataDt',
        key: 'dataDt',
        title: '对比日期',
        width: 120,
        render: (text, record, index) => {
          if (text === null || text === undefined) {
            return '--';
          } else {
            return text.substring(0, 4) + '-' + text.substring(4, 6) + '-' + text.substring(6, 8);
          }
        },
      },
      {
        dataIndex: 'fundcode',
        key: 'fundcode',
        title: '产品代码',
        width: 180,
        render: (val, record) => {
          if (record.isExpire == 0) {
            return <span style={{ color: 'red' }}>{val}</span>;
          } else {
            return val;
          }
        },
      },
      {
        dataIndex: 'fundname',
        key: 'fundname',
        width: 180,
        title: '产品名称',
      },
      {
        dataIndex: 'combiCode',
        key: 'combiCode',
        width: 180,
        title: '组合代码',
      },
      {
        dataIndex: 'combiName',
        key: 'combiName',
        width: 180,
        title: '组合名称',
      },
      {
        dataIndex: 'cousetitle',
        key: 'cousetitle',
        title: '现金科目',
        width: 100,
        render: (val) => {
          let cousetitle = val.split('_')[1];
          if (cousetitle == '无') {
            return '--';
          } else {
            return val;
          }
        },
      },
      {
        dataIndex: 'cz',
        key: 'cz',
        type: 'number',
        toFixed: 2,
        width: 120,
        title: '差值',
        render: (text, record, index) => {
          if (record.cousetitle && record.cousetitle.substr(record.cousetitle.length - 1, 1) == '无') {
            return '--';
          }
          return accounting.formatNumber(Math.abs(text), 2);
        },
      },
      {
        dataIndex: 'xtsl',
        key: 'xtsl',
        type: 'number',
        toFixed: 2,
        width: 120,
        title: '系统数量',
      },
      {
        dataIndex: 'cwsl',
        key: 'cwsl',
        type: 'number',
        toFixed: 2,
        width: 120,
        title: '财务数量',
      },
    ],
    xtcolumns: [
      {
        dataIndex: 'date',
        key: 'date',
        title: '交易日期',
        type: 'date',
      },
      {
        dataIndex: 'miniUnitId',
        key: 'miniUnitId',
        title: '组合',
      },
      {
        dataIndex: 'type',
        key: 'type',
        title: '交易方向',
      },
      {
        dataIndex: 'amount',
        key: 'amount',
        title: '成交数量',
        type: 'number',
      },
      {
        dataIndex: 'price',
        key: 'price',
        title: '成交金额',
        type: 'money',
        toFixed: 2,
      },
    ],
    cwcolumns: [
      {
        dataIndex: 'date',
        key: 'date',
        title: '交易日期',
        type: 'date',
      },
      {
        dataIndex: 'type',
        key: 'type',
        title: '交易方向',
      },
      {
        dataIndex: 'amount',
        key: 'amount',
        title: '成交数量',
        type: 'number',
      },
      {
        dataIndex: 'price',
        key: 'price',
        title: '成交金额',
        type: 'money',
        toFixed: 2,
      },
    ],
  },
  effects: {
    //获取持仓核对表格数据
    *getPositionTableData({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationCheck.getPositionTableData, payload);

      if (res && res.code === 0) {
        // 处理dataSource
        res.data.dataSource &&
          res.data.dataSource.map((item, index) => {
            item.id = new Date().getTime() + index;
          });
        callback('success', res.data);
      } else {
        callback('fail');
        message.error(res?.msg || '获取持仓核对表格数据失败');
      }
    },
    //获取资金核对表格数据
    *getFundTableData({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationCheck.getFundTableData, payload);
      if (res && res.code === 0) {
        callback('success', res.data);
      } else {
        callback('fail');
        message.error(res?.msg || '获取资金核对表格数据失败');
      }
    },
    // 批量调平
    *batchLevel({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationCheck.batchLevel, payload);

      if (res && res.success == true) {
        callback('success');
        message.success('批量调平成功');
      } else {
        callback('fail');
        message.error(res?.message || '批量调平失败');
      }
    },
    // 持仓调平
    *adjustSecurity({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationCheck.adjustSecurity, payload);
      if (res && res.code == 0) {
        callback(true);
        message.success('调平成功');
      } else {
        message.error(res.msg || '调整失败');
      }
    },
    //持仓回溯
    *Backtracking({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationCheck.Backtracking, payload);
      if (res && res.code == 0) {
        callback(true);
        message.success('回溯成功');
      } else {
        message.error(res.msg || '回溯失败');
      }
    },
    // 批量回溯
    *batchBack({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationCheck.batchBack, payload);

      if (res && res.success == true) {
        callback('success');
        message.success('批量回溯成功');
      } else {
        callback('fail');
        message.error(res?.message || '批量回溯失败');
      }
    },
    // 获取流水对比
    *getturnover({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationCheck.getturnover, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.msg || '获取流水对比失败');
      }
    },
    // 更新未读消息
    *updateUnreadDate({ payload }, { call, put }) {
      const res = yield call(api.updateUnreadDate, payload);
      if (!res || res.code != 0) {
        message.error(`更新未读消息失败，失败原因：${res?.msg}`);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
